package com.kevin.test.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 
 */
public class CglibAopDemo {

    public static void main(String[] args) {
        byteCodeGe();
    }

    public static void byteCodeGe() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Business.class);
        enhancer.setCallback(new LogIntercept());
        IBusiness2 newBusiness = (IBusiness2) enhancer.create();
        newBusiness.doSomeThing2();
    }

    /**
     */
    public static class LogIntercept implements MethodInterceptor {

        public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            Object rev = proxy.invokeSuper(target, args);
            if (method.getName().equals("doSomeThing2")) {
                System.out.println("LogIntercept");
            }
            return rev;
        }
    }
}
